# -*- codeing = utf-8 -*-
# @Time : 2023/1/14 15:59
# @Author : 小马
# @File: plant_vs_zoomie_game_normal_main.py
# @Software : PyCharm
import time

import pygame
import random
import os
from Adc import Adc
from shooter import shoot
from Ap import Ap
from tank import tank
from Sun import Sun
from Zombie import Zombie
from badmanmini import badmanmini
from Bullet import Bullet
from bulletshooter import bulletshooter
from enemy import enemy
from Carman import Carman
from Boss import Boss

from pythonExtend import pythonExtend


pygame.init()
#1680*720
backgd_size=(1200,600)
screen=pygame.display.set_mode(backgd_size)
pygame.display.set_caption('保卫花花之家')
bg_image_path= 'material/bg1.1.png'
bg_img_obj=pygame.image.load(bg_image_path).convert_alpha()

# sunbank_image_path= 'material/counter2.jpeg'
# sunbank_img_obj=pygame.image.load(sunbank_image_path).convert_alpha()

sunbankImg=pygame.image.load('material/cardbank.png').convert_alpha()
deleteImg=pygame.image.load('material/delete1.png').convert_alpha()
delete=pygame.image.load('material/delete.png').convert_alpha()

cardadc=pygame.image.load('material/cardadc.png').convert_alpha()
cardap=pygame.image.load('material/cardap.png').convert_alpha()
cardtank=pygame.image.load('material/cardtank.png').convert_alpha()
cardshooter=pygame.image.load('material/cardshooter.png').convert_alpha()

apImg=pygame.image.load('material/character3.1.PNG').convert_alpha()
tankImg=pygame.image.load('material/character4.1.PNG').convert_alpha()
adcImg=pygame.image.load('material/character1.1.1.PNG').convert_alpha()
shooterImg=pygame.image.load('material/character2.1.PNG').convert_alpha()




text='900'
sun_font=pygame.font.SysFont('arial',25)
sun_num_surface=sun_font.render(text,True,(0,0,0))




# adc=Adc()
# ap=Ap()
# tank=tank()
#shoot=shoot()
# zombie=Zombie()
# badman=badmanmini()
# enemy=enemy()



spriteGroup=pygame.sprite.Group()
#spriteGroup.add(adc)
# spriteGroup.add(ap)
# spriteGroup.add(tank)
#spriteGroup.add(shoot)
# spriteGroup.add(zombie)
# spriteGroup.add(badman)
# spriteGroup.add(enemy)



sunList=pygame.sprite.Group()
bulletList=pygame.sprite.Group()
enemyList=pygame.sprite.Group()



clock=pygame.time.Clock()
GEN_SUN_EVENT=pygame.USEREVENT+1
pygame.time.set_timer(GEN_SUN_EVENT,1000)

GEN_BULLET_EVENT=pygame.USEREVENT+2
pygame.time.set_timer(GEN_BULLET_EVENT,3000)

GEN_ENERMY_EVENT=pygame.USEREVENT+3
pygame.time.set_timer(GEN_ENERMY_EVENT,9000)



choose=0 #点中太阳花为1 点中坚果为2 点中射手为3 点中ADC为4
def init():
 pygame.mixer.init()
 s='material/sound'
 bgm=pygame.mixer.Sound(os.path.join(s,'bgm1.mp3'))
 start=pygame.mixer.Sound(os.path.join(s,'start1.mp3'))
 start2=pygame.mixer.Sound(os.path.join(s,'start2.mp3'))
 bossbgm=pygame.mixer.Sound(os.path.join(s,'boss.mp3'))
 tank1=pygame.mixer.Sound(os.path.join(s,'tank.mp3'))
 pygame.mixer.Sound.play(start)
 pygame.mixer.Sound.play(start2)
 sounds=[]

 global text,choose
 global sun_num_surface
 running =True
 index=0
 while running:
    # sounds.append(bgm)
    # for sound in sounds:
    #  pygame.mixer.Sound.play(sound)

    pygame.mixer.Sound.play(bgm)

    clock.tick(10)

    # if index % 40==0:
    #     s=Sun(ap.rect)
    #     sunList.add(s)
    # if index % 40==0:
        # boss = Boss()
        # spriteGroup.add(boss)
        # for i in range(1,5):
        #     j = random.randint(1, 3)
        #     if j==1:
        #         zombie = Zombie()
        #         badman = badmanmini()
        #         ene = enemy()
        #         spriteGroup.add(zombie)
        #         spriteGroup.add(badman)
        #         spriteGroup.add(ene)
        #     elif j==2:
        #         badman = badmanmini()
        #         e = enemy()
        #
        #         spriteGroup.add(badman)
        #         spriteGroup.add(e)
        #     elif j==3:
        #         en = enemy()
        #         carman=Carman()
        #
        #         spriteGroup.add(en)
        #         spriteGroup.add(carman)






    # if index %80==0:
    #     b=Bullet(adc.rect,backgd_size)
    #     spriteGroup.add(b)
    # if index %30==0:
    #     d=bulletshooter(shoot.rect,backgd_size)
    #     spriteGroup.add(d)

    for bullet in bulletList:
        for enem in enemyList:
            if pygame.sprite.collide_mask(bullet,enem):
                bulletList.remove(bullet)
                enem.blood-=1
    for item in spriteGroup:
        for enem in enemyList:
            if  isinstance(item,tank) and pygame.sprite.collide_mask(item,enem):
                enem.blood-=1
                item.blood-=1



    screen.blit(bg_img_obj,(0,0))
    screen.blit(sunbankImg,(320,-12))

    screen.blit(sun_num_surface,(350,57))
    screen.blit(cardap,(415,3))
    screen.blit(cardadc, (491,3))
    screen.blit(cardshooter, (567,3))
    screen.blit(cardtank, (643,3))
    screen.blit(deleteImg, (719, 3))


    spriteGroup.update(index)
    spriteGroup.draw(screen)
    sunList.update(index)
    sunList.draw(screen)
    bulletList.update(index)
    bulletList.draw(screen)
    enemyList.update(index)
    enemyList.draw(screen)

    (x,y)=pygame.mouse.get_pos()
    if choose==1:
        right=apImg.get_rect().right
        bottom=apImg.get_rect().bottom
        screen.blit(apImg,(x-right/2,y-bottom/2))

    elif choose==4:
        right = apImg.get_rect().right
        bottom = apImg.get_rect().bottom
        screen.blit(tankImg,(x-right/2,y-bottom/2))
    elif choose==2:
        right = apImg.get_rect().right
        bottom = apImg.get_rect().bottom
        screen.blit(adcImg,(x-right/2,y-bottom/2))
    elif choose == 3:
        right = apImg.get_rect().right
        bottom = apImg.get_rect().bottom
        screen.blit(shooterImg, (x-right/2,y-bottom/2))
    elif choose==5:
        right = apImg.get_rect().right
        bottom = apImg.get_rect().bottom
        screen.blit(delete,(x-right/2,y-bottom/2))




    # screen.blit(adc.images[index%8],adc.rect)
    # screen.blit(shoot.images[index%31],shoot.rect)
    # screen.blit(ap.images[index % 26], ap.rect)
    # screen.blit(tank.images[index % 31], tank.rect)

    # for sun in sunList:
    #     screen.blit(sun.images[index % 1], sun.rect)


    index+=1
    for event in pygame.event.get():
        if event.type==GEN_SUN_EVENT:
            for sprite in spriteGroup:
                if isinstance(sprite,Ap):
                    now=time.time()
                    if now-sprite.lasttime>=5:
                        s = Sun(sprite.rect)
                        sunList.add(s)
                        sprite.lasttime=now


        if event.type==GEN_BULLET_EVENT:
            for sprite in spriteGroup:
                if isinstance(sprite,Adc):
                    now=time.time()
                    if now-sprite.lasttime>=5:
                       b=Bullet(sprite.rect,backgd_size)
                       bulletList.add(b)
                       sprite.lasttime=now
                elif isinstance(sprite,shoot):
                    bs=bulletshooter(sprite.rect,backgd_size)
                    bulletList.add(bs)

        if event.type==GEN_ENERMY_EVENT:

            boss = Boss()
            enemyList.add(boss)
            for i in range(1, 5):
                j = random.randint(1, 3)
                if j == 1:
                    zombie = Zombie()
                    badman = badmanmini()
                    ene = enemy()
                    enemyList.add(zombie)
                    enemyList.add(badman)
                    enemyList.add(ene)
                elif j == 2:
                    badman = badmanmini()
                    e = enemy()

                    enemyList.add(badman)
                    enemyList.add(e)
                elif j == 3:
                    en = enemy()
                    carman = Carman()
                    pythonextend=pythonExtend()

                    enemyList.add(en)
                    enemyList.add(carman)
                    enemyList.add(pythonextend)




        if event.type==pygame.QUIT:
               running=False
        if event.type==pygame.MOUSEBUTTONDOWN:
            pressed_point=pygame.mouse.get_pressed()
            if pressed_point[0]==1:
                position=pygame.mouse.get_pos()
                x,y=position
                print(position)
                if 415<x<486 and 3<y<84 and int (text)>=50:
                    choose=1
                elif 491<x<562 and 3<y<84 and int (text)>=100:
                    choose=2
                elif 567<x<638 and 3<y<84 and int (text)>=100:
                    choose=3
                elif 643<x<714 and 3<y<84 and int (text)>=50:
                    choose=4
                elif  719<x<771 and 3<y<83:
                    choose=5

                elif 425<x<1200 and 100<y<600:
                    if choose==1:
                        current_time=time.time()
                        ap=Ap(current_time)
                        i=425
                        while(i<=1200):
                            if i<=x<=i+85:
                                ap.rect.left=i
                                break
                            i=i+85
                        j=100
                        while(j<=600):
                            if j<=y<=j+100:
                                ap.rect.top=j
                                break
                            j=j+100

                        #=positiony
                        #=positionx
                        spriteGroup.add(ap)
                        choose=0
                        text = str(int(text) - 50)
                        sun_font = pygame.font.SysFont('arial', 25)
                        sun_num_surface = sun_font.render(text, True, (0, 0, 0))
                        # text=int(text)
                        # text-=50
                        # text=str(text)
                        # myfont=pygame.font.SysFont('arial',20)
                        # txtImg= myfont.render(str(text),True,(0,0,0))
                    elif choose==4:
                        # pygame.mixer.init()
                        # s = 'material/sound'
                        # bgm = pygame.mixer.Sound('material/sound/tank.mp3')
                        # bgm.play()
                        t = tank()
                        i = 425
                        while (i <= 1200):
                            if i <= x <= i + 85:
                                t.rect.left = i
                                break
                            i = i + 85
                        j = 100
                        while (j <= 600):
                            if j <= y <= j + 100:
                                t.rect.top = j
                                break
                            j = j + 100

                        # =positiony
                        # =positionx
                        spriteGroup.add(t)
                        # pygame.mixer.Sound.play(tank1)
                        choose = 0
                        text = str(int(text) - 50)
                        sun_font = pygame.font.SysFont('arial', 25)
                        sun_num_surface = sun_font.render(text, True, (0, 0, 0))

                    elif choose==2:
                        current_time = time.time()
                        adc = Adc(current_time)
                        i = 425
                        while (i <= 1200):
                            if i <= x <= i + 85:
                                adc.rect.left = i
                                break
                            i = i + 85
                        j = 100
                        while (j <= 600):
                            if j <= y <= j + 100:
                                adc.rect.top = j
                                break
                            j = j + 100

                        # =positiony
                        # =positionx
                        spriteGroup.add(adc)
                        choose = 0
                        text = str(int(text) - 50)
                        sun_font = pygame.font.SysFont('arial', 25)
                        sun_num_surface = sun_font.render(text, True, (0, 0, 0))
                    elif choose==3:
                        sh=shoot()
                        i = 400
                        while (i <= 1175):
                            if i <= x <= i + 85:
                                sh.rect.left = i
                                break
                            i = i + 85
                        j = 80
                        while (j <= 580):
                            if j <= y <= j + 100:
                                sh.rect.top = j
                                break
                            j = j + 100

                        # =positiony
                        # =positionx
                        spriteGroup.add(sh)
                        # pygame.mixer.Sound.play(tank1)
                        choose = 0
                        text = str(int(text) - 50)
                        sun_font = pygame.font.SysFont('arial', 25)
                        sun_num_surface = sun_font.render(text, True, (0, 0, 0))

                    elif choose==5:
                        for sprite in spriteGroup:
                            if sprite.rect.left<x<sprite.rect.right and sprite.rect.top<y<sprite.rect.bottom:
                                spriteGroup.remove(sprite)
                                choose=0
                            else:
                                choose=0










                for sun in sunList:
                    if sun.rect.collidepoint(position):
                        sunList.remove(sun)
                        text=str(int(text)+50)
                        sun_font = pygame.font.SysFont('arial', 25)
                        sun_num_surface = sun_font.render(text, True, (0, 0, 0))
    pygame.display.update()

if __name__=="__main__":
    init()
